package com.liuzhilong.infrastructure.basic.proxy.dynamic;

import com.liuzhilong.infrastructure.basic.proxy.RealSubject;
import com.liuzhilong.infrastructure.basic.proxy.dynamic.cglib.SimpleCGLIBProxy;
import org.springframework.cglib.core.DebuggingClassWriter;
import org.springframework.cglib.core.DefaultGeneratorStrategy;
import org.springframework.cglib.proxy.Enhancer;
import sun.misc.ProxyGenerator;

import java.io.*;

public class ExportClass {
    public static void main(String[] args) throws Exception {
        exportJdkProxy();
        exportcglibProxy();
    }
    //导出jdk代理
    private static final void exportJdkProxy() throws IOException {
        String filepath = "$Proxy0.class";
        int v=17;
        byte[] codeBytes = ProxyGenerator.generateProxyClass("com.sun.proxy.$Proxy0", RealSubject.class.getInterfaces(),v);
        File file = new File(filepath);
        if(!file.exists()){
            file.createNewFile();
        }
        try(OutputStream outputStream = new FileOutputStream(filepath)){
            outputStream.write(codeBytes);
        }


    }

    //导出cglib代理
    private static final void exportcglibProxy() throws Exception {
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\cglib\\gen");
        RealSubject proxiedInfo = SimpleCGLIBProxy.create(RealSubject.class);

    }

}
